// packageJ/category_child/category_child.js
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    supplier: false,
    language:"",
    goods: [],
    category: [],
    cat_set: "",
    view:true,
    top_img: "",
    sort_name: 1,
    sort_status: 1,
    goods_name: "",
    goods_style: "01",
    posterId:"",
    page: 1, //分页数，当前页数
    isLoadMore: true, //判断是否要加载更多的标志
    total_page: 0, //总页数
    cate_id: "",
    init_cate_id:"",
    imgPath: app.globalData.imgPath,
    head_height:"",
    posterShow: false, //海报显示
    posterImg: "" //海报数据
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    console.log(options,"options");
    let language = wx.getStorageSync("langIndex");
    this.setData({
      language: language.en
    });
    this.data.init_cate_id = options.id;
    this.data.cate_id = options.id;
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },
  bannerClick(){

  },
  

  /**
   * 生命周期函数--监听页面显示
   */
  onPageScroll(){
    console.log("=====")
    let query = wx.createSelectorQuery(); 
    query.select('.search-box-sticky').boundingClientRect(rect=>{ this.setData({head_height:rect.height})}).exec();
  },
  onShow() {
    this.getData();
  },
  gotoDetail(e){
    console.log(e,"e====")
  },
  postShow(e){
    let id = e.currentTarget.dataset.goodsid;
    this.getShare(id);
    this.setData({
      posterId: id,
      posterShow: true
    });
  },
  copybtn(e) {
    let inviteCode = e.target.dataset.title;
    let goods_link = e.target.dataset.link;
    if (goods_link) {
      let aa = goods_link.split('0x0A');
      let bb = aa.join('\r');
      wx.setClipboardData({
        data: goods_link ? bb : '',
      });
    } else {
      wx.setClipboardData({
        data: inviteCode ? inviteCode : '',
      });
    }
  },
  showbox(){
    this.setData({view:!this.data.view});
  },
  getData() {
    app._getNetWork({
      url:app.getNetAddresss('goods.category.getThirdCategory'),
      data:{
        cate_id:this.data.cate_id,
        sort_name:this.data.sort_name,
        sort_status:this.data.sort_status,
        goods_name:this.data.goods_name
      },
      success:resdata=>{
        let {result,msg,data} = resdata.data;
        console.log(result,data);
        if(result!==1){
          return;
        }
        let category = data.category;
        if(category&&category.length!==0){
          let obj = {
            id: this.data.init_cate_id,
            name: "全部商品",
            thumb: "https://mini-app-img-1251768088.cos.ap-guangzhou.myqcloud.com/classificationicon@2x.png"
          };
          category.push(obj);
        }
        let category_10 = this.chunk(category, 10);
        this.setData({
          goods_style:data.goods_style,
          top_img:data.top_img,
          goods:data.goods.data,
          cat_set:data.cat_set,
          category:category_10
        })
        this.data.total_page = data.goods.last_page || 0;
      }
    })
  },
  chunk(arr, n) {
    var result = [];
    if(arr&&arr.length!==0){
      for (var i = 0, len = arr.length; i < len; i += n) {
        result.push(arr.slice(i, i + n));
      }
    }
    return result;
  },
  intbtn(e){
    let id =e.currentTarget.dataset.id;
    this.setData({cate_id:id});
    this.getData();
  },
  toggle(e) {
    let idx = e.currentTarget.dataset.idx;
    switch (idx) {
    case 1:
      this.setData({
        hascolor: 1,
        sort_name:this.data.supplier?5:'id'
      });
      if (this.data.colorshow == '1_2') {
        this.setData({
          colorshow: '1_1',
          sort_status:2,
        });
      } else {
        this.setData({
          colorshow: '1_2',
          sort_status:1,
        });
    
      }
      break;
    case 2:
      this.setData({
        hascolor: 2,
        sort_name:2
      });
      if (this.data.colorshow == '2_2') {
        this.setData({
          colorshow: '2_1',
          sort_status:2,
        });
      } else {
        this.setData({
          colorshow: '2_2',
          sort_status:1,
        });
      }
      break;
    case 3:
      this.setData({
        hascolor: 3,
        sort_name:3
      });
      if (this.data.colorshow == '3_2') {
        this.setData({
          sort_status:2,
          colorshow: '3_1'
        });
      } else {
        this.setData({
          sort_status:1,
          colorshow: '3_2'
        });
      }
      break;
    }
    this.getData();
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },
  onClose(){
    this.setData({posterShow:false});
  },
  search_change(e){
    this.setData({goods_name:e.detail});
  },
})